

struct ListNode {
     int val;
     struct ListNode *next;
 };

struct ListNode *detectCycle(struct ListNode *head) {
    struct ListNode *slow=head,*fast=head;
    while(fast&&fast->next)
    {
        fast=fast->next->next;
        slow=slow->next;
        if(fast==slow)
        {
            while(fast!=head)
            {
                head=head->next;
                fast=fast->next;

            }return fast;
        }
    }
    return fast;
}